package com.skycong.hello77.common.okhttp3;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * Created by Kiki on 2018/6/15.
 */

public class CommonRequest {
    /**
     * post请求
     * @param url
     * @param params
     * @return
     */
    public static Request createPostRequest(String url, RequestParams params){
        FormBody.Builder mFormBodyBuild = new FormBody.Builder();
        if(params != null){
            for(Map.Entry<String,String> entry:params.urlParams.entrySet()){
                mFormBodyBuild.add(entry.getKey(),entry.getValue());
            }
        }
        //通过请求构件类的build方法获取到真正的请求体对象
        FormBody mFormBody = mFormBodyBuild.build();
        return new Request.Builder().url(url).post(mFormBody).build();
    }

    /**
     * get请求带参数
     * @param url
     * @param params
     * @return
     */
    public static Request createGetRequest(String url,RequestParams params){
        StringBuilder urlBuilder = new StringBuilder(url).append("?");
        if(params != null){
            for(Map.Entry<String,String> entry:params.urlParams.entrySet()){
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        return new Request.Builder().url(urlBuilder.substring(0,urlBuilder.length()-1)).get().build();
    }

    /**
     * get请求不带参数
     * @param url
     * @return
     */
    public static Request createGetWithoutParams(String url){
        return new Request.Builder().url(url).get().build();
    }
}
